package com.kd.juhaowan.ui.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;


import com.hjq.http.listener.OnHttpListener;
import com.hjq.toast.ToastUtils;
import com.kd.juhaowan.base.BoxCacheManager;
import com.kd.juhaowan.http.api.exception.TokenException;
import com.kd.juhaowan.ui.AuthActivity;
import com.kd.juhaowan.ui.BaseActivity;
import com.kd.juhaowan.ui.LoginAccountActivity;
import com.kd.juhaowan.ui.MainActivity;
import com.kd.juhaowan.ui.PlatFormCoinActivity;
import com.kd.juhaowan.utils.LogUtil;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseFragment extends Fragment implements OnHttpListener {
    private View mRootView;

    private boolean isReCreateView;
    private Unbinder mUnbinder;
    BoxCacheManager manager;
    protected ActivityResultLauncher<Intent> baseFragmentRegister;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
         super.onCreateView(inflater, container, savedInstanceState);
        this.isReCreateView = mRootView != null;
        if (mRootView == null) {
            mRootView = doInflateContentView(inflater, container, getViewLayoutResourceId());
        }

        if (isSavedInstanceState()) {
            ViewGroup parent = (ViewGroup) mRootView.getParent();
            if (parent != null) {
                parent.removeView(mRootView);
            }
        }

        mUnbinder = ButterKnife.bind(this, mRootView);
        baseFragmentRegister = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                LogUtil.debugLog( "onActivityResult: _____" + result);
                if (result != null) {

                    if (result.getResultCode() == Activity.RESULT_OK) {
                        reInitDate();

                    }
                }
            }
        });
        if(!isReCreateView){
            manager = BoxCacheManager.getInstence(getContext());
            onButterKnifeCreateViewAfter();
        }
        return mRootView;
    }
    protected boolean isSavedInstanceState() {
        return false;
    }
    protected abstract int getViewLayoutResourceId();
    protected abstract void reInitDate();
    protected void onButterKnifeCreateViewAfter() {
    }
    protected View doInflateContentView(LayoutInflater inflater, ViewGroup root, int resource) {
        if (inflater == null) {
            if (!isNullContext()) {
                inflater = LayoutInflater.from(getActivity());
            } else {
                return null;
            }
        }
        return inflater.inflate(resource, root, false);
    }

    protected boolean isNullContext() {
        return getActivity() == null;
    }

    @Override
    public void onHttpSuccess(Object result) {

        LogUtil.debugLog("base Fragment success");
    }

    @Override
    public void onHttpFail(Exception e) {
//        ToastUtils.show(e.getMessage());
        LogUtil.debugLog("base faild"+e.getMessage());
        if(e instanceof TokenException){
            if(BaseFragment.this instanceof MineFragment)return;
            manager.writeAutoLogin(false);
            baseFragmentRegister.launch(new Intent(getActivity(), LoginAccountActivity.class));
//            startActivity();
//            getActivity().finish();
        }else {
            ToastUtils.show(e.getMessage());

            LogUtil.debugLog("base faild"+e.getMessage());

        }
    }
}
